/*  hccctocsy_CA_nnh_pfg_lek_600_cp

  1H(t1)  > 13C (43 ppm) > flopsy > 13Ca to 15N (58 ppm)
     > 15N (t2) to HN (t3)

  This sequence will enable assignment of sidechains of residues that are (i-1) to PRO

  To calibrate:

   a: 13C carrier at 43 ppm
    13C high power pulse, pwc = p2 at pl2
        CO decoupling, pl23, using onepulse.sp23

       flopsy transfer uses pl22 and p22

   b: 13C carrier at 58 ppm
       pwcreb - reb pulse at 43 ppm - should hit methyls to Thr Cb 73 - 15 ppm

       If desired (-DCbdec 13C decoupling with pl27 and onepulse.sp27

       off resonance decoupling of 13CO using pl26 and onepulse.sp26

  Note: Make sure that when using Bax/Logan trick the d0 is large enough to accomodate
   what you subtract.

  Written by LEK on May 10, 2022 from ccctocsy_nnh_pfg_lek_800_cp
 */

#include <Avance.incl>
#include <Grad.incl>
#include <Delay.incl>

;Define phases
#define zero ph=0.0
#define one ph=90.0
#define two ph=180.0
#define three ph=270.0

;Define Pulses
define pulse pwh
       "pwh=p1"
define pulse pwmlev
       "pwmlev=p12"
define pulse pwcreb
       "pwcreb=p25"
define pulse pwn
       "pwn=p3"
define pulse pwc
       "pwc=p2"
define pulse pwcdip
       "pwcdip=p22"

;#ifdef -DCbdec
;define pulse pwcbdec  ; wurst for selective Cb dec
;       "pwcbdec=p27"
;#endif

define pulse pwd
       "pwd=p4"
define pulse pwddec
       "pwddec=p41"

;Define delays
"d11=30m"
"in0=inf1/2"
"in10=inf2/2"

define delay taua
       "taua = d20"      ; < 1/4JCH ~ 1.8 ms

define delay taub
       "taub = d21"     ; ~ 0.3/JCH * 0.5,  1.05 ms

define delay tauc
       "tauc = d22"       ; 2tauc is time for transfer from 13C to 15N

define delay bigTN
       "bigTN = d25"     ; < 1/4JNCO delay, N constant time period

define delay tauf
       "tauf = d26"       ; < 1/4JNH, ~ 2.2 ms

define delay zeta
define delay zeta1
       "zeta1=0.1u"
 
define delay bigT1
     "bigT1 = d14"  ; 650 us

define delay hscuba
      "hscuba = 30m"

;Define flags
;define f1180  ; set zgoptns -Df1180
#ifdef f1180
 "d0= (in0/2)"    
#else
 "d0=0.2u"
#endif

;define f2180  ; set zgoptns -Df2180
#ifdef f2180
 "d10=(in10/2)"
#else
 "d10=0.2u"
#endif

"d30 = 0.2u"  ; redefined later in program

;"acqt0=0"
;aqseq 321     ; Determines the order of acquisition when 'mc' macro is used.

define loopcounter ncyc
   "ncyc = l7"

#ifdef oneDarray
#else
define loopcounter ni
       " ni = td1/2"
define loopcounter zetacount
       "zetacount=1"
define loopcounter ni2
       "ni2 = td2/2"
#endif

"l10 = 1"

/* Assign cnsts to check validity of parameter range */
#ifdef fsat
  "cnst10 = plw10" ; tsatpwr - set max at 0.8 W  
#endif
  "cnst12=plw12" ; tpwrml - set max at 0.8W
  "cnst22=plw22" ; dpwrdip - set max at 65W
  "cnst23=plw23" ; dpwrcodec - set max at 10.0W
  "cnst25=spw25" ; power level for reburp pulse 
  "cnst26=plw26" ; CO decoupling during 15N t2 

#ifdef Cbdec
  "cnst27=plw27" ; dpwrcbdec - set max at 2.0W
#endif

  "cnst31=plw31" ; dpwr2 - set max at 6W
#ifdef Ddec
  "cnst4=plw4" ; dpwr3 pl4 - set max at 10.5W for 2H pulses
  "cnst41=plw41" ; dpwr3dec pl41 - set max at 1.5W
#endif

/* BEGIN ACTUAL PULSE SEQUENCE */

1 ze

; check validity of parameters

  if "d0 < 0.2u "
  {
   "d0=0.2u"
  }

  if "d10 < 0.2u "
  {
   "d10=0.2u"
  }

#ifdef fsat
   if "cnst10 > 0.5" 
  {
   2u
   print "error: tsatpwr, pl10 is incorrect"
   goto HaltAcqu
  }
#endif

  if "p22 < 24u" 
  {
   2u
   print "error: pwcdip p22 incorrect"
   goto HaltAcqu
  }

  if "cnst12 > 1.5"
  {
   2u
   print "error: tpwrml pl12 incorrect"
   goto HaltAcqu
  }

  if "cnst22 > 37.0"
  {
   2u
   print "error: dpwrdip pl22 incorrect"
   goto HaltAcqu
  }

  if "cnst23 > 1.0"
  {
   2u
   print "error: dpwrcodec pl23/spw23 for CO seduce dec t1  incorrect"
   goto HaltAcqu
  }

  if "cnst25 > 140.0"
  {
   2u
   print "error: power level for reburp is too large "
   goto HaltAcqu
  }

  if "cnst26 > 1.0"
  {
   2u
   print "error: power level for 13CO decoupling during 15N t2 acq is too large"
   goto HaltAcqu
  }

#ifdef Cbdec
  if "cnst27 > 2.0"
  {
   2u
   print "error: dpwrcbdec pl27/spw27 for Cb wurst(90) dec incorrect"
   goto HaltAcqu
  }
#endif

  if "cnst31 > 4.0"
  {
   2u
   print "error: dpwr2 pl31 incorrect"
   goto HaltAcqu
  }

  if "ncyc > 9"
  {
   2u
   print "error: ncyc is too large"
   goto HaltAcqu
  }

  if "pwcdip*94.22*ncyc > 30m"
  {
   2u
   print "error: dipsi time is too large"
   goto HaltAcqu
  }

#ifdef Ddec
  if "cnst4 > 10.5"
  {
   2u
   print "error: dpwr3 pl4 too large"
   goto HaltAcqu
  }

  if "cnst41 > 2.0"
  {
   2u
   print "error: dpwr3dec pl41 too large"
   goto HaltAcqu
  }

  if "pwd < 100.0u"
  {
   2u
   print "error: pwd is too short > 100 us"
   goto HaltAcqu
  }

  if "pwddec < 300.0u"
  {
   2u
   print "error: pwddec is too short > 300 us"
   goto HaltAcqu
 }

;  d11  LOCKDEC_ON
  50u LOCKH_ON    ; turn on lock hold
  d11 H2_PULSE    ; put lock to pulse mode
  2u pl41:f4
#endif

2 d11 do:f3

#ifdef Ddec    /* D decoupling */
d11 H2_LOCK    ; put lock channel in lock mode
6m LOCKH_OFF   ; turn off lock hold

#ifdef fsat                           ; zgoptn -Dfsat
  4u pl10:f1                          ; power(tsatpwr) for presaturation
  d1 cw:f1 zero                       ; Hcw(d1)x
  4u do:f1                            ; cw off
  2u pl1:f1                           ; power(tpwr)

#ifdef fscuba                   /* Scuba pulse sequence */
   hscuba                          ; delay(hscuba)
    (pwh zero):f1                  ; H 90x180y90x
    (pwh*2 one):f1
    (pwh zero):f1
   hscuba                          ; delay(hscuba)
#endif                           /* end fscuba */

#else                             /* if fsat is no */
 2u pl1:f1                        ; power(tpwr)
 d1                               ; delay(d1)
#endif                            /* end if fsat */

50u  LOCKH_ON
15u H2_PULSE

#else    /* no D decoupling */

#ifdef fsat                           ; zgoptn -Dfsat
  4u pl10:f1                          ; power(tsatpwr) for presaturation
  d1 cw:f1 zero                       ; Hcw(d1)x
  4u do:f1                            ; cw off
  2u pl1:f1                           ; power(tpwr)

#ifdef fscuba                   /* Scuba pulse sequence */
   hscuba                          ; delay(hscuba)
    (pwh zero):f1                  ; H 90x180y90x
    (pwh*2 one):f1
    (pwh zero):f1
   hscuba                          ; delay(hscuba)
#endif                           /* end fscuba */

#else                             /* if fsat is no */
 2u pl1:f1                        ; power(tpwr)
 d1                               ; delay(d1)
#endif                            /* end if fsat */

#endif                            /* end of D_decoupling */

  2u pl3:f3
  2u fq=0:f2                             ;13C SFO2 @ 43ppm
  2u pl2:f2                              ;power for C90
  2u pl1:f1   

; ensure magnetization originates on 1H not on C or N
  (pwc zero):f2  
  2u
  (pwn zero):f3  

#ifdef Ddec
  20u UNBLKGRAMP ; this unblanks the gradients, lock is already in hold mode from above
#else
  20u UNBLKGRAD  ; this unblanks the gradients and puts lock in hold mode
#endif

  2u
  (p50:gp0)                      
  d16

/* Bax Logan conditions*/

  if "ni > 1" {
      "zeta = (taua + pwc*2 - 2u - p51 - d16 - 5u)/(ni - 1)"
    }
   else {
      "zeta = 0.1u"
  }

  if " zeta < 0.1u "
  {
   2u
   print "error: problem with zeta"
   goto HaltAcqu
  }

#ifdef f1180
   "d30 = d0 - in0/2"
   "zetacount = trunc(d30/in0 + 0.4)"
   "zeta1 = zeta*zetacount"
#else
   "zetacount = trunc(d0/in0 + 0.4)"
   "zeta1 = zeta*zetacount"
#endif

if "zeta > in0" {
     "zeta1 = 0.2u"
}

/* end of Bax Logan conditions*/

; This is the real start

 (pwh ph10):f1                                 ; H90x

  2u
  p51:gp1                                       ; gradient 1
  d16

  "DELTA = taua + d0 - 2.0u - p51 - d16"
   DELTA

  (pwc*2 zero):f2

  "DELTA = d0 - zeta1"
   DELTA

   (pwh*2 zero):f1

   "DELTA = taua - zeta1 + pwc*2 - 2u - p51 - d16"
    DELTA

  2u
  p51:gp1                                       ; gradient 1
  d16

  (pwh one):f1                                  ; H90y

/* start 2H decoupling if desired */

#ifdef Ddec
  2u pl4:f4
  (pwd one):f4
  2u pl41:f4
  (2u zero):f4
  (2u cpds4 zero):f4                          ;2H dec ON
#endif

 (pwc ph1):f2                                  ; C90(ph1)

  2u pl23:f2

  /* Turn ON CO seduce decoupling */
  (2u cpds5 zero):f2                            ; CO dec ON (off res seduce)

#ifdef Ddec
  "DELTA = taub - 2u - 2u - 2u - 4u - pwd - 2u - p52 - d16 - 2u"
   DELTA
#else
  "DELTA = taub - 2u - 2u - 2u - 2u - p52 - d16 - 2u"
   DELTA
#endif

/* CO decoupling off */
  2u do:f2                                      ; CO dec off     

#ifdef Ddec
  2u do:f4                                      ; 2H dec OFF
  2u pl4:f4
  (pwd three):f4
#endif

  2u
  p52:gp2                                       ; Gradient 2
  d16                                     

  2u pl2:f2                                          ; power for C 180
  (center (pwh*2 ph26):f1 (pwc*2 ph26):f2 )
  2u pl23:f2                                         ; power for CO seduce dec

  2u
  p52:gp2                                             ; gradient 2
  d16

#ifdef Ddec
  2u pl4:f4
  (pwd one):f4
  2u pl41:f4
  (2u zero):f4
  (2u cpds4 zero):f4                          ;2H dec ON
#endif

  /* Turn ON CO seduce decoupling */
  (2u cpds5 zero):f2                            ; CO dec ON (off res seduce)

#ifdef Ddec
  "DELTA = taub - 2u - 2u - p52 - d16 - 8u - pwd - 2u - 2u - 2u"
#else
  "DELTA = taub - 2u - 2u - p52 - d16 - 2u - 2u - 2u"
#endif
  DELTA

/* Turn off CO decoupling */
  2u do:f2                                      ; CO dec off     

  2u pl2:f2                                     ; power for C90
  (pwc one):f2                                  ; C 90y

/* Turn off 2H decoupling if it was on */
#ifdef Ddec
  2u do:f4                                      ; 2H dec OFF
  2u pl4:f4
  (pwd three):f4
#endif

if "l10 % 2 == 1" {
    (pwh two):f1
}
else {
    "DELTA = pwh"
 DELTA
}

  2u
  p53:gp3                                             ; gradient 3
  d16 pl22:f2       

/* Tocsy transfer time */

  if "ncyc >= 1" 
  {
    "DELTA = pwcdip*94.22*ncyc"
    (DELTA cpds2 zero):f2                                ; cpds2="flopsy8" on
     2u do:f2                                          ; tocsy off 
   }

/* end Tocsy transfer */
 
  2u fq=cnst2:f2                           ; 13C SFO2+cnst2(Hz) @ 58ppm

  2u
  p53:gp3                                            ; gradient 3
  d16 pl2:f2                                        ; power for 13C at high power

  "DELTA = 3.4m - 6u - p53 - d16 "
   DELTA

/* now start 1H decoupling */

   2u pl12:f1                                       ; power for 1H dec
   (pwmlev one):f1                                  ; H90y @ powe pl12
   (2u cpds1 zero):f1                               ; waltz16x ON

#ifdef Ddec
  2u pl4:f4
  (pwd one):f4
  2u pl41:f4
  (2u zero):f4
  (2u cpds4 zero):f4                          ;2H dec ON
#endif

; transfer from 13Ca to 15N 
; use high power 13Ca pulses at 58 ppm

  (pwc ph2):f2                        

#ifdef Cbdec

 /* 13Cb decoupling */ 
  2u pl27:f2                                         ; power for Cb decoupling
  (2u cpds7 zero):f2                                 ;  Cb dec ON

  "DELTA = tauc - 2u - 2u - 2u" 
  DELTA

/* Turn off 13Cb decoupling */
  2u do:f2                                            ; Cb dec off
/* Turn off 13Cb decoupling */

  (pwcreb:sp25 ph26):f2                              ;  43 ppm
  1u
  (pwn*2.0 ph26):f3

  2u pl27:f2                                         ; power for Cb decoupling
  (2u cpds7 zero):f2                                 ;  Cb dec ON

  "DELTA = tauc - 1u - pwn*2.0 - 2u - 2u - 2u - 2u" 
  DELTA

  2u do:f2                                            ; Cb dec off
  2u pl2:f2                                         ; power for Ca 90b
  (pwc ph27):f2                                   ; Ca90y
#else
  "DELTA = tauc" 
  DELTA

  (pwcreb:sp25 ph26):f2                              ; 43 ppm
  1u
  (pwn*2.0 ph26):f3

  "DELTA = tauc - 1u - pwn*2.0 - 2u" 
  DELTA

  2u pl2:f2                                         ; power for Ca 90b
  (pwc ph27):f2                                   ; Ca90y
#endif

  /* Turn OFF 1H Waltz dec */
  2u do:f1                                           ; 1H dec OFF
  /* Waltz dec off */
  (pwmlev three):f1                                  ; H90-y at pl12

/* Turn off 2H decoupling if needed */
#ifdef Ddec
  2u do:f4                                      ; 2H dec OFF
  2u pl4:f4
  (pwd three):f4
  2u pl41:f4
#endif

  2u
  p54:gp4                                             ; gradient4 
  d16 

; now transfer from 15N to HN for detection with acq of 15N

/* now start 1H decoupling */

   2u pl12:f1                                       ; power for 1H dec
   (pwmlev one):f1                                  ; H90y @ powe pl12
   (2u cpds1 zero):f1                               ; waltz16x ON

  /* Turn ON CO seduce decoupling */
  2u pl26:f2                                  
  (2u cpds6 zero):f2                        

if "bigTN - d10 > 0.2u" {

  (pwn ph7):f3                                  ; N90(PH7)

  "DELTA = bigTN - d10"                         ; dly 1/(4JNCO) - t2/2
  DELTA

/* Turn off 13CO dec */
  2u do:f2                   
  2u pl2:f2                

  (pwn*2 ph8):f3                                ; N180(PH8)
   1u
  (pwc*2.0 ph26):f2     

  /* Turn ON CO seduce decoupling */
  2u pl26:f2                        
  (2u cpds6 zero):f2              

  "DELTA = bigTN + 2u + 2u - 1u - pwc*2.0 -2u -2u + d10 - 5.5m"   
  DELTA
}
else {
  (pwn ph7):f3                                  ; N90(PH7)
  1u
  (pwn*2 ph8):f3

  "DELTA = d10 - bigTN"            
  DELTA

/* Turn off 13CO dec */
  2u do:f2                 
  2u pl2:f2                 

  (pwc*2.0 zero):f2     

  /* Turn ON CO seduce decoupling */
  2u pl26:f2                       
  (2u cpds6 zero):f2                            ; Ca dec ON (on res seduce)

  "DELTA = bigTN + 1u - 2u - 2u - pwc*2.0  -2u -2u + d10 - 5.5m"   
   DELTA
}
0.2u

  /* Turn OFF 1H decoupling */
  2u do:f1                                      ; H dec off
  (pwmlev three):f1                               ; H90-x at pl12

  "DELTA = 5.5m - 0.2u - 2u - pwmlev - 2u - 2u - 2u - p56 - 1.0m - 2u"
   DELTA

  /* Turn OFF CO decoupling */
  2u do:f2                               
  2u pl2:f2                          

  2u
  p56:gp6                      ; gradient6 coherence selection 
  1m
  
  2u pl1:f1                                ; power for pwh

  (center (pwh ph26):f1 (pwn ph9):f3)        ; H90x,N90(ph9)

  (pwc zero):f2

  2u
  p57:gp7                                    ; gradient 7
  d16

  "DELTA = tauf - 2u - pwc - 2.0u - p57 - d16"            ; dly 1/4JNH
  DELTA
 
  (center (pwh*2 ph26):f1 (pwn*2 ph26):f3)   ; H180x,N180x

  "DELTA = tauf - 2.0u - p57 - d16"            ; dly 1/4JNH
  DELTA

  2u
  p57:gp7                                    ; gradient 7
  d16

  (center (pwh ph27):f1 (pwn ph29):f3)         ; H90y,N90y

  "DELTA = tauf - 2.0u - p58 - d16 - (pwn - pwh)*0.5"            ; dly 1/4JNH
  DELTA

  2u
  p58:gp8                                    ; gradient 8
  d16
 
  (center (pwh*2 ph26):f1 (pwn*2 ph26):f3)   ; H180x,N180x

  2u
  p58:gp8                                    ; gradient 8
  d16

  "DELTA = tauf - 2.0u - p58 - d16"            ; dly 1/4JNH
  DELTA

  (pwh zero):f1                              ; H90x

  "DELTA = bigT1 - pwh*2/PI"
  DELTA                                     ; dly DELTA

  (pwh*2 zero):f1

  2u
  p59:gp9*EA                                    ; gradient 9 coherence selection
  d16                                 

#ifdef Ddec
  4u BLKGRAMP                                ; Blank gradients
#else
  4u BLKGRAD                                 ; Blank gradients and lock on
#endif

  2u pl31:f3

  "DELTA = bigT1 - 2u - p59 - d16 - 4u - 2u - de"
  DELTA                                     ; dly DELTA

  go=2 ph31 cpd3:f3
  d11 do:f3 mc #0 to 2 
  F1PH(calph(ph10, +90) & calclc(l10,1) , caldel(d0, +in0)) 
  F2EA(calgrad(EA) & calph(ph9, +180) , calph(ph7, +180) & calph(ph31, +180) &  caldel(d10, +in10))

#ifdef Ddec
d11 H2_LOCK
d11 LOCKH_OFF
; d11 LOCKDEC_OFF
#endif

HaltAcqu, 1m
exit

ph1=0 2      
ph2=0         
ph7=0 0 2 2
ph8=0 0 0 0 2 2 2 2
ph9=0
ph10=0
ph31=0 2 2 0
ph26=0
ph27=1
ph28=2
ph29=3

;cnst2 : (58-43)ppm offset in Hz
;pl1 : tpwr - power level for pwh
;pl10 : tsatpwr - power level for 1H presat
;pl12 : tpwrml - power level for 1H waltz16
;pl2 : dhpwr - power level for C high power 90 and 180
;pl21 : dpwr - low power level for C (dec)
;pl22 : dpwrdip - power level for C tocsy
;pl23 : dpwrcodec - power level for CO seduce decoupling t1
;pl25 : power level for C reb ~ <=350 u centered at 43 ppm
;pl26 : CO decoupling 15N magnetization -  t2
;pl27 : dpwrcbdec - power level for sel Cb decoupling
;pl3 : dhpwr2 - power level for pwn
;pl31 : dpwr2 - power level for 15N cpd
;pl4  : power level for 2H pulses
;pl41 : power level for 2H decoupling
;sp25 : power level for reburp
;p1 : pwh
;p12 : pwmlev - 1H 90 for waltz16dec at power pl12
;p2 : pwc
;p22 : pwcdip - 13C 90 at pl22 for C tocsy about 30 ms
;p23 : pwCOdec - pattern length us @ spw23 during t1
;p25 : pwcreb
;p26 : pattern length for decoupling of CO during 15N t2
;p27 : pwCbdec - pattern length us @ spw27
;p3 : pwn
;p31 : pwn at dpwr2 for decoupling
;p4 : 2H high power pulse, 90
;p41 : 2H decoupling
;d1 : relaxation delay
;d11 : delay for disk i/o, 30ms
;d16 : gradient recovery delay, 200us
;d14 : bigT1, 650 us
;d20 : taua, H to C, 1.8ms
;d21 : taub, refocus JHC for 13C mag
;d22 : tauc,  Ca to N transfer ~ 13 ms unless Cb dec
;d25 : bigTN, t2 evolv, N-CO refocus, 12.4 ms
;d26 : tauf, 1/4JNH, ~ 2.4 ms
;d30 : dummy variable - do not adjust
;cpds1: 1H decoupling according to program defined by cpdprg1 (waltz16)
;cpds2: C decoupling for tocsy according to program defined by cpdprg2 (flopsy8)
;cpd3 : 15N decoupling according to program defined by cpdprg3 (waltz16)
;cpds4 : 2H decoupling according to program defined by cpdprg4 (waltz16)
;cpds5: CO decoupling t1 according to program defined by cpdprg5 (onepulse.sp23 off res seduce)
;cpds6: CO decoupling t2 according to program defined by cpdprg6 (onepulse.sp26)
;cpds7: Cb decoupling according to program defined by cpdprg7 (wurst2)
;pcpd3: 1/dmf2 - 90 degree pulse for cpd3
;l7 : ncyc, number of times flopsy is repeated; total_time = pwcdip*94.22*ncyc
;spnam23 : File name for CO decoupling sitting at 43 ppm t1
;spnam26 : File name for CO decoupling during 15N t2
;spnam27 : File name for Cb decoupling during Ca to N transfer
;cpdprg2 : flopsy transfer
;cpdprg5 : CO decoupling while sit at 43 ppm
;cpdprg6 : CO decoupling while carbon carrier at 58 ppm (mag is 15N)
;cpdprg7 : Cb decoupling while sit at 58 ppm
;zgoptns : Df1180,Df2180,DDdec, DoneDarray,DCbdec 

